using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using server.status_db;
using EFCore.BulkExtensions;
using Microsoft.Extensions.Logging;
using System.Dynamic;
using System.Linq.Dynamic.Core;

[Authorize]
public class JxgzlController : ODataController
{
    private status_oneContext _db;
    readonly ILogger<AlfxsController> _log;
    public JxgzlController(status_oneContext context, ILogger<AlfxsController> log)
    {
        _db = context;
        _log = log;

    }
    private bool 教学工作量Exists(Guid key)
    {
        return _db.JS_B_教学工作量.Any(p => p.ID == key);
    }
    [HttpPut]
    public async Task<IActionResult> Put([FromODataUri] Guid key, [FromBody]JS_B_教学工作量 update)
    {
        try
        {
            if (!ModelState.IsValid)
            {

                var message = string.Join(" | ", ModelState.Values
                               .SelectMany(v => v.Errors)
                               .Select(e => e.Exception.Message));
                _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500, message);
            }
            if (key != update.ID)
            {
                return StatusCode(500, "key not eq id");
            }
            _db.Entry(update).State = EntityState.Modified;
            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException e)
            {

                var message = "多用户不能同时修改同一条数据";
                _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500, message);

            }
            return Updated(update);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    public async Task<IActionResult> Delete([FromODataUri] Guid key)
    {

        try
        {
            var mc = await _db.JS_B_教学工作量.FindAsync(key);
            if (mc == null)
            {
                var message = "没有发现要删除的对象Id";
                _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500, message);
            }
            _db.JS_B_教学工作量.Remove(mc);
            await _db.SaveChangesAsync();
            return NoContent();
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] JS_B_教学工作量 model)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                var message = string.Join(" | ", ModelState.Values
                                .SelectMany(v => v.Errors)
                                .Select(e => e.Exception.Message));
                return StatusCode(500, message);
            }
            _db.JS_B_教学工作量.Add(model);
            await _db.SaveChangesAsync();

            return Created(model);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
      [HttpGet("/api/Jxgzl/GetData")]
    public IActionResult GetData(String XXID, String CJPC,String query,string order,int limit,int skip,String addQuery)
    {
        //
        Console.WriteLine("query "+query);
        //
         var ghs =  _db.JS_B_教师基本情况.Where("XXID == @0 and CJPC == @1 ",XXID,CJPC).Where(addQuery).Select(e=>e.GH).ToArray();
        
         var datas = _db.JS_B_教学工作量.Where("XXID == @0 and CJPC == @1 ",XXID,CJPC).Where(e=>ghs.Contains(e.GH));
         if(query != null)
         datas = datas.Where(query);
    
        //
        //
        dynamic temp = new  ExpandoObject();
       
        temp.total = datas.Count();
        temp.values = datas.OrderBy(order).Skip(skip).Take(limit).ToArray();
        
        return this.Ok(temp);
    }
    [EnableQuery]
    public IActionResult Get()
    {
        try
        {
            return Ok(_db.JS_B_教学工作量);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    //
}